package main

import (
	//"crypto/tls"
	"log"
	"net/http"
	"net/url"
	"sync"
	"time"
)

const (

	//目标网站
	targetUrl = "https://httpbin.org/ip"

	//隧道服务地址
	sd_api = "ng001.weimiaocloud.com:9003"
)

func main() {

	//持续时间（单位：秒）
	c0 := 10

	//并发数
	c1 := 5
	wg := sync.WaitGroup{}

	//c0 = iArgs[3]
	//c1 = iArgs[4]

	for i := 0; i < c1; i++ {
		wg.Add(1)
		go func() {
			for i := 0; i < c0; i++ {
				go getData("http://" + sd_api)
				time.Sleep(time.Second)

			}
			wg.Done()
		}()
	}
	wg.Wait()
	//time.Sleep(time.Second * 60)

}

func getData(api_addr string) {
	log.Printf("时间:%v, 协程开始\n", time.Now().Format("2006-01-02 15:04:05"))
	sdApi, err := url.Parse(api_addr)
	if err != nil {
		log.Fatal("url.Parse", err)
	}

	netTransport := &http.Transport{
		Proxy:                 http.ProxyURL(sdApi),
		MaxIdleConnsPerHost:   5,
		ResponseHeaderTimeout: time.Second * time.Duration(30),
		//TLSClientConfig:       &tls.Config{InsecureSkipVerify: true}, //忽略https验证
	}

	httpClient := &http.Client{
		Timeout:   time.Second * 60,
		Transport: netTransport,
	}

	request, _ := http.NewRequest("GET", targetUrl, nil)
	request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46")
	start := time.Now()
	response, err := httpClient.Do(request)
	if err != nil {
		log.Println("状态:访问超时 ", err)
		return
	}

	//关闭
	defer response.Body.Close()

	if response.StatusCode != http.StatusOK {
		log.Println(err)
		return
	}

	log.Printf("时间:%v, 状态:%v, 耗时: %v\n", time.Now().Format("2006-01-02 15:04:05"), response.Status, time.Since(start))
}
